

#import "FAMHomeTableViewHeaderView.h"
#import <math.h>
@interface FAMHomeTableViewHeaderView()
@property (weak, nonatomic) IBOutlet UILabel *FAMFoodKcalLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMBaseGoalLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMMonthWithDayLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMWeakLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMFatLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMCarbsLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMProteinLabel;
@property (weak, nonatomic) IBOutlet UIView *FAMPNCircleChartBackView;
@property (nonatomic, strong)PNCircleChart *FAMCircleChart;
@property (nonatomic, strong)UILabel *FAMProportionLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMMaintainWeightLabel;

@end

@implementation FAMHomeTableViewHeaderView
- (instancetype)initWithFrame:(CGRect)frame{
    self =  [super initWithFrame:frame];
    if (self) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FAMHomeTableViewHeaderView" owner:self options:nil];
        self = array.firstObject;
        self.FAMMonthWithDayLabel.text = [NSString stringWithFormat:@"%@ %@",[FAMTool FAM_getMonth:[NSDate date]],[FAMTool FAM_getDay:[NSDate date]]];
        self.FAMWeakLabel.text = [FAMTool FAM_getWeak:[NSDate date]];
        
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 568);
}
- (PNCircleChart *)FAMCircleChart {
    if (!_FAMCircleChart) {
        _FAMCircleChart = [[PNCircleChart alloc]initWithFrame:CGRectMake(0, 0, self.FAMPNCircleChartBackView.frame.size.width, self.FAMPNCircleChartBackView.frame.size.height) total:@100 current:@0 clockwise:NO shadow:YES shadowColor:kCustom0xColor(0xF7F8F9, 1.0) displayCountingLabel:YES overrideLineWidth:@15];
        _FAMCircleChart.chartType = PNChartFormatTypePercent;
        _FAMCircleChart.strokeColor = kCustom0xColor(0xF7F8F9, 1.0);
        [_FAMCircleChart strokeChart];
        _FAMCircleChart.countingLabel.textColor = kClearColor;
        CGRect frame = _FAMCircleChart.countingLabel.frame;
        frame.size.height = 60;
        _FAMCircleChart.countingLabel.frame = frame;
        [_FAMCircleChart.countingLabel addSubview:self.FAMProportionLabel];
        self.FAMProportionLabel.sd_layout.leftEqualToView(_FAMCircleChart.countingLabel).rightEqualToView(_FAMCircleChart.countingLabel).bottomEqualToView(_FAMCircleChart.countingLabel).topEqualToView(_FAMCircleChart.countingLabel);
        [self.FAMPNCircleChartBackView addSubview:_FAMCircleChart];
    }
    return _FAMCircleChart;
}
- (UILabel *)FAMProportionLabel {
    if (!_FAMProportionLabel) {
        _FAMProportionLabel = [[UILabel alloc]init];
        _FAMProportionLabel.numberOfLines = 0;
        _FAMProportionLabel.textAlignment = NSTextAlignmentCenter;
        _FAMProportionLabel.font = [UIFont systemFontOfSize:10];
    }
    return _FAMProportionLabel;
}
- (IBAction)FAM_weightTapClick:(UITapGestureRecognizer *)sender {
    [UIWindow.currentViewController.navigationController pushViewController:[FAMHomeGoalsViewController new] animated:YES];
}
- (IBAction)FAM_baseGoalTapClick:(UITapGestureRecognizer *)sender {
    [UIWindow.currentViewController.navigationController pushViewController:[FAMHomeBasalMetabolismViewController new] animated:YES];
}

- (void)setFAMFoodListModel:(FAMFoodrecordListModel *)FAMFoodListModel {
    _FAMFoodListModel = FAMFoodListModel;
    CGFloat totaleKcal = 0;
    CGFloat fat = 0;
    CGFloat carbs = 0;
    CGFloat protein = 0;
    for (int i = 0; i<FAMFoodListModel.breakfast.count; i++) {
        FAMFoodrecordModel *model = FAMFoodListModel.breakfast[i];
        totaleKcal += model.calories.floatValue;
        fat += model.fat.floatValue;
        carbs += model.carbs.floatValue;
        protein += model.protein.floatValue;
    }
    for (int i = 0; i<FAMFoodListModel.lunch.count; i++) {
        FAMFoodrecordModel *model = FAMFoodListModel.lunch[i];
        totaleKcal += model.calories.floatValue;
        fat += model.fat.floatValue;
        carbs += model.carbs.floatValue;
        protein += model.protein.floatValue;
    }
    for (int i = 0; i<FAMFoodListModel.dinner.count; i++) {
        FAMFoodrecordModel *model = FAMFoodListModel.dinner[i];
        totaleKcal += model.calories.floatValue;
        fat += model.fat.floatValue;
        carbs += model.carbs.floatValue;
        protein += model.protein.floatValue;
    }
    for (int i = 0; i<FAMFoodListModel.extra.count; i++) {
        FAMFoodrecordModel *model = FAMFoodListModel.extra[i];
        totaleKcal += model.calories.floatValue;
        fat += model.fat.floatValue;
        carbs += model.carbs.floatValue;
        protein += model.protein.floatValue;
    }
    self.FAMFoodKcalLabel.attributedText = [FAMTool FAM_AttributedString:[NSString stringWithFormat:@"%0.1f",totaleKcal] styleTwoString:@"kcal" styleColorOne:kBlackColor styleColorTwo:kCustom0xColor(0x757575, 1.0) styleFontOne:22 styleFontTwo:16];
    
    self.FAMFatLabel.text = [NSString stringWithFormat:@"%0.1f/%0.1fg",(fat*0.3)/9,fat];
    self.FAMCarbsLabel.text = [NSString stringWithFormat:@"%0.1f/%0.1fg",(carbs*0.5)/4,carbs];
    self.FAMProteinLabel.text = [NSString stringWithFormat:@"%0.1f/%0.1fg",(protein*0.2)/4,protein];
    
}
- (void)setFAMFoodmateUserdataModel:(FAMGetFoodmateUserdataModel *)FAMFoodmateUserdataModel {
    _FAMFoodmateUserdataModel = FAMFoodmateUserdataModel;
    self.FAMBaseGoalLabel.text = FAMFoodmateUserdataModel.basic_meta;
    if ([FAMFoodmateUserdataModel.target_type isEqualToString:@"1"]) {
        self.FAMMaintainWeightLabel.text = @"Lose weight";
    }else if ([FAMFoodmateUserdataModel.target_type isEqualToString:@"2"]){
        self.FAMMaintainWeightLabel.text = @"Gain weight";
    }else{
        self.FAMMaintainWeightLabel.text = @"Maintain weight";
    }
    CGFloat totaleKcal = 0;
    for (int i = 0; i<_FAMFoodListModel.breakfast.count; i++) {
        FAMFoodrecordModel *model = _FAMFoodListModel.breakfast[i];
        totaleKcal += model.calories.floatValue;
    }
    for (int i = 0; i<_FAMFoodListModel.lunch.count; i++) {
        FAMFoodrecordModel *model = _FAMFoodListModel.lunch[i];
        totaleKcal += model.calories.floatValue;
    }
    for (int i = 0; i<_FAMFoodListModel.dinner.count; i++) {
        FAMFoodrecordModel *model = _FAMFoodListModel.dinner[i];
        totaleKcal += model.calories.floatValue;
    }
    for (int i = 0; i<_FAMFoodListModel.extra.count; i++) {
        FAMFoodrecordModel *model = _FAMFoodListModel.extra[i];
        totaleKcal += model.calories.floatValue;
    }
    CGFloat remaining = FAMFoodmateUserdataModel.basic_meta.floatValue - totaleKcal;
    self.FAMCircleChart.total = @100;
    if (remaining == FAMFoodmateUserdataModel.basic_meta.floatValue) {
        self.FAMCircleChart.current = @100;
        self.FAMProportionLabel.attributedText = [FAMTool FAM_AttributedString:@"100%\n" styleTwoString:@"Still edible" styleColorOne:kCustom0xColor(0x151212, 1.0) styleColorTwo:kCustom0xColor(0x757575, 1.0) styleFontOne:18 styleFontTwo:8];
    }else if(remaining < 0){
        self.FAMCircleChart.circleBackground.strokeColor = kCustom0xColor(0x33D078, 1.0).CGColor;
        CGFloat proportion = remaining/FAMFoodmateUserdataModel.basic_meta.floatValue;
        self.FAMCircleChart.current = @(fabs(proportion*100));
        self.FAMCircleChart.strokeColor = kCustom0xColor(0xFF5324, 1.0);
        self.FAMCircleChart.circleBackground.backgroundColor = kCustom0xColor(0x33D078, 1.0).CGColor;
        self.FAMProportionLabel.attributedText = [FAMTool FAM_AttributedString:[NSString stringWithFormat:@"%0.1f%%\n",fabs(proportion*100)] styleTwoString:@"exceed" styleColorOne:kCustom0xColor(0x151212, 1.0) styleColorTwo:kCustom0xColor(0xFF5324, 1.0) styleFontOne:18 styleFontTwo:8];
    }else if (remaining > 0 && remaining < FAMFoodmateUserdataModel.basic_meta.floatValue){
        self.FAMCircleChart.strokeColor = kCustom0xColor(0xF7A912, 1.0);
        CGFloat proportion = totaleKcal / FAMFoodmateUserdataModel.basic_meta.floatValue;
        self.FAMCircleChart.current = @(proportion*100);
        self.FAMProportionLabel.attributedText = [FAMTool FAM_AttributedString:[NSString stringWithFormat:@"%0.1f%%\n",proportion * 100] styleTwoString:@"Can still" styleColorOne:kCustom0xColor(0x151212, 1.0) styleColorTwo:kCustom0xColor(0x757575, 1.0) styleFontOne:18 styleFontTwo:8];
    }
    [self.FAMCircleChart strokeChart];
    
}
- (IBAction)FAM_timerTapClick:(UITapGestureRecognizer *)sender {
    FAMHomeTimerView *timerView = [[FAMHomeTimerView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    WeakSelf(self);
    timerView.block = ^(NSDate * _Nonnull date) {
        weakSelf.FAMMonthWithDayLabel.text = [NSString stringWithFormat:@"%@ %@",[FAMTool FAM_getMonth:date],[FAMTool FAM_getDay:date]];
        weakSelf.FAMWeakLabel.text = [FAMTool FAM_getWeak:date];
    };
    UIWindow *window = [[UIApplication sharedApplication] windows].lastObject;
    [window addSubview:timerView];
}
- (IBAction)FAM_DailyDietaryReportTapClick:(UITapGestureRecognizer *)sender {
    [UIWindow.currentViewController.navigationController pushViewController:[FAMMemberDailyNnalysisViewController new] animated:YES];
}
- (IBAction)FAM_vipButtonClick:(UIButton *)sender {
    [UIWindow.currentViewController.navigationController pushViewController:[FAMMemberVipViewController new] animated:YES];
}


@end
